﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using System.IO;

namespace Election.NguoiDiBau
{
	public partial class Form_NguoiDiBau_Main : Form
	{
		private string MaTV;
		private string MaNBB;
		private static OracleConnection conn = orcl.conn;

		public Form_NguoiDiBau_Main(string MaTV)
		{
			InitializeComponent();
			this.MaTV = MaTV;
		}

		private void listDotBauCu_SelectedIndexChanged(object sender, EventArgs e)
		{
			btTai_Click(null, null);
		}

		private void Form_NguoiDiBau_Main_Load(object sender, EventArgs e)
		{
			// tải danh sách đợt bầu cử
			OracleCommand comm = new OracleCommand();
			comm.Connection = conn;
			comm.CommandText = "select MaDotBauCu from QUANLYBAUCU.DOTBAUCU where ThoiGianKetThuc > (select sysdate from dual) order by MaDotBauCu desc";
			try
			{
				OracleDataReader reader = comm.ExecuteReader();
				if (reader.Read())
					listDotBauCu.Items.Add(reader["MaDotBauCu"].ToString());
				else
					listDotBauCu.Items.Add("Hiện tại không có đợt bầu cử nào");
				while (reader.Read())
				{
					listDotBauCu.Items.Add(reader["MaDotBauCu"].ToString());
				}
				listDotBauCu.SelectedIndex = 0;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				this.Close();
			}
			listDotBauCu.SelectedIndexChanged += new EventHandler(listDotBauCu_SelectedIndexChanged);
		}

		private void Form_NguoiDiBau_Main_Shown(object sender, EventArgs e)
		{
			btTai_Click(null, null);
		}

		private void btTai_Click(object sender, EventArgs e)
		{
			checkUCV1.CheckState = checkUCV2.CheckState = checkUCV3.CheckState = checkUCV4.CheckState = checkUCV5.CheckState = CheckState.Unchecked;
			infoUCV1.Text = infoUCV2.Text = infoUCV3.Text = infoUCV4.Text = infoUCV5.Text = "";
			imgUCV1.Image = imgUCV2.Image = imgUCV3.Image = imgUCV4.Image = imgUCV5.Image = null;
			
			// kiểm tra mình có trong ds người bình bầu ko ?
			OracleCommand comm = new OracleCommand();
			comm.Connection = conn;
			comm.CommandText = "select MaNBB from QUANLYBAUCU.VIEW_NGUOIDIBAU_NBB where MaTV = '" + MaTV + "' and MaDotBauCu = '" + listDotBauCu.SelectedItem.ToString() + "'";
			try
			{
				OracleDataReader reader = comm.ExecuteReader();
				if (reader.Read())
				{
					MaNBB = reader["MaNBB"].ToString();

					// tải danh sách UCV
					OracleCommand comm2 = new OracleCommand();
					comm2.Connection = conn;
					comm2.CommandType = CommandType.StoredProcedure;
					comm2.CommandText = "QUANLYBAUCU.PROC_NGUOIDIBAU_UCV";
					comm2.Parameters.Add("MaDBC", OracleDbType.Varchar2).Value = listDotBauCu.SelectedItem.ToString();
					comm2.Parameters.Add("p_cursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output; 
					OracleDataReader reader2 = comm2.ExecuteReader();

					// UCV 1
					reader2.Read();
					checkUCV1.Text = reader2["MaUCV"].ToString();
					infoUCV1.Text = "Họ tên : " + reader2["Ten"].ToString() + "\r\n" +
						"Phái : " + reader2["Phai"].ToString() + "\r\n" +
						"Quê quán : " + reader2["QueQuan"].ToString() + "\r\n" +
						"Ngày sinh : " + reader2["NgaySinh"].ToString() + "\r\n" +
						"*****\r\nQUÁ TRÌNH CÔNG TÁC\r\n-----\r\n" + reader2["QuaTrinhCongTac"].ToString();
					if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".png"))
						imgUCV1.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".png");
					else if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg"))
						imgUCV1.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg");

					// UCV 2
					reader2.Read();
					checkUCV2.Text = reader2["MaUCV"].ToString();
					infoUCV2.Text = "Họ tên : " + reader2["Ten"].ToString() + "\r\n" +
						"Phái : " + reader2["Phai"].ToString() + "\r\n" +
						"Quê quán : " + reader2["QueQuan"].ToString() + "\r\n" +
						"Ngày sinh : " + reader2["NgaySinh"].ToString() + "\r\n" +
						"*****\r\nQUÁ TRÌNH CÔNG TÁC\r\n-----\r\n" + reader2["QuaTrinhCongTac"].ToString();
					if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".png"))
						imgUCV2.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".png");
					else if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg"))
						imgUCV2.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg");

					// UCV 3
					reader2.Read();
					checkUCV3.Text = reader2["MaUCV"].ToString();
					infoUCV3.Text = "Họ tên : " + reader2["Ten"].ToString() + "\r\n" +
						"Phái : " + reader2["Phai"].ToString() + "\r\n" +
						"Quê quán : " + reader2["QueQuan"].ToString() + "\r\n" +
						"Ngày sinh : " + reader2["NgaySinh"].ToString() + "\r\n" +
						"*****\r\nQUÁ TRÌNH CÔNG TÁC\r\n-----\r\n" + reader2["QuaTrinhCongTac"].ToString();
					if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".png"))
						imgUCV3.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".png");
					else if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg"))
						imgUCV3.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg");

					// UCV 4
					reader2.Read();
					checkUCV4.Text = reader2["MaUCV"].ToString();
					infoUCV4.Text = "Họ tên : " + reader2["Ten"].ToString() + "\r\n" +
						"Phái : " + reader2["Phai"].ToString() + "\r\n" +
						"Quê quán : " + reader2["QueQuan"].ToString() + "\r\n" +
						"Ngày sinh : " + reader2["NgaySinh"].ToString() + "\r\n" +
						"*****\r\nQUÁ TRÌNH CÔNG TÁC\r\n-----\r\n" + reader2["QuaTrinhCongTac"].ToString();
					if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".png"))
						imgUCV4.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".png");
					else if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg"))
						imgUCV4.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg");

					// UCV 5
					reader2.Read();
					checkUCV5.Text = reader2["MaUCV"].ToString();
					infoUCV5.Text = "Họ tên : " + reader2["Ten"].ToString() + "\r\n" +
						"Phái : " + reader2["Phai"].ToString() + "\r\n" +
						"Quê quán : " + reader2["QueQuan"].ToString() + "\r\n" +
						"Ngày sinh : " + reader2["NgaySinh"].ToString() + "\r\n" +
						"*****\r\nQUÁ TRÌNH CÔNG TÁC\r\n-----\r\n" + reader2["QuaTrinhCongTac"].ToString();
					if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".png"))
						imgUCV5.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".png");
					else if (File.Exists(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg"))
						imgUCV5.Image = Image.FromFile(orcl.imagePath + reader2["MaUCV"].ToString() + ".jpg");

					reader2.Close();
					reader2.Dispose();
					comm2.Dispose();
				}
				else
				{
					MessageBox.Show("Bạn không có trong danh sách người bình bầu của đợt bầu cử này!\nXin hãy chọn đợt bầu cử khác!");
				}
				reader.Close();
				reader.Dispose();
				comm.Dispose();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void btLuu_Click(object sender, EventArgs e)
		{
			int count = 0;
			if (checkUCV1.CheckState == CheckState.Checked)
				count++;
			if (checkUCV2.CheckState == CheckState.Checked)
				count++;
			if (checkUCV3.CheckState == CheckState.Checked)
				count++;
			if (checkUCV4.CheckState == CheckState.Checked)
				count++;
			if (checkUCV5.CheckState == CheckState.Checked)
				count++;
			if (count != 3)
			{
				MessageBox.Show("Phải chọn 3 trong 5 ứng cử viên!");
				return;
			}
			OracleCommand comm = new OracleCommand();
			comm.CommandType = CommandType.StoredProcedure;
			comm.Connection = conn;
			try
			{
				// xóa kết quả bầu cũ
				comm.CommandText = "QUANLYBAUCU.PROC_NGUOIDIBAU_BAU_XOA";
				comm.Parameters.Add("nguoibb", OracleDbType.Varchar2).Value = MaNBB;
				comm.ExecuteNonQuery();

				// thêm kết quả bầu mới
				if (checkUCV1.CheckState == CheckState.Checked)
				{
					comm.CommandText = "QUANLYBAUCU.PROC_NGUOIDIBAU_BAU_THEM";
					comm.Parameters.Clear();
					comm.Parameters.Add("nguoibb", OracleDbType.Varchar2).Value = MaNBB;
					comm.Parameters.Add("ucv", OracleDbType.Varchar2).Value = checkUCV1.Text;
					comm.ExecuteNonQuery();
				}
				if (checkUCV2.CheckState == CheckState.Checked)
				{
					comm.CommandText = "QUANLYBAUCU.PROC_NGUOIDIBAU_BAU_THEM";
					comm.Parameters.Clear();
					comm.Parameters.Add("nguoibb", OracleDbType.Varchar2).Value = MaNBB;
					comm.Parameters.Add("ucv", OracleDbType.Varchar2).Value = checkUCV2.Text;
					comm.ExecuteNonQuery();
				}
				if (checkUCV3.CheckState == CheckState.Checked)
				{
					comm.CommandText = "QUANLYBAUCU.PROC_NGUOIDIBAU_BAU_THEM";
					comm.Parameters.Clear();
					comm.Parameters.Add("nguoibb", OracleDbType.Varchar2).Value = MaNBB;
					comm.Parameters.Add("ucv", OracleDbType.Varchar2).Value = checkUCV3.Text;
					comm.ExecuteNonQuery();
				}
				if (checkUCV4.CheckState == CheckState.Checked)
				{
					comm.CommandText = "QUANLYBAUCU.PROC_NGUOIDIBAU_BAU_THEM";
					comm.Parameters.Clear();
					comm.Parameters.Add("nguoibb", OracleDbType.Varchar2).Value = MaNBB;
					comm.Parameters.Add("ucv", OracleDbType.Varchar2).Value = checkUCV4.Text;
					comm.ExecuteNonQuery();
				}
				if (checkUCV5.CheckState == CheckState.Checked)
				{
					comm.CommandText = "QUANLYBAUCU.PROC_NGUOIDIBAU_BAU_THEM";
					comm.Parameters.Clear();
					comm.Parameters.Add("nguoibb", OracleDbType.Varchar2).Value = MaNBB;
					comm.Parameters.Add("ucv", OracleDbType.Varchar2).Value = checkUCV5.Text;
					comm.ExecuteNonQuery();
				}
				MessageBox.Show("Bạn đã bầu chọn thành công");
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			comm.Dispose();
		}
	}
}
